package com.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.ssm.base.model.Page;
import com.ssm.domain.Address;
import com.ssm.service.IAddressService;
import com.ssm.util.mybatis.ExampleUtil;
import com.ssm.util.mybatis.ExampleType.Operation;

/**
*
*suae  2016年7月17日
*@return
*/
@Controller
@RequestMapping("address")
public class AddressController {

	@Autowired
	private IAddressService addressService;
	
	
	/**
	 * 鏌ヨ鍟嗗搧鍒楄〃
	 * 
	 * @author suae
	 * @Date  2016骞�7鏈�6鏃�20:34
	 * 
	 * @param model
	 * @param Goods
	 * @param page
	 * @return
	 */
	@RequestMapping("query")
	public @ResponseBody PageInfo<Address> query(Model model, Address address, Page page) {
		Object example = ExampleUtil.getInstance().addParam("city",address.getCity(), Operation.LIKE)//////// 宸ュ叿閲岄潰鏈夋敞閲�
				.addOrder("id")////// 鎺掑簭 鑷繁鎵嬪啓鍒嗙粍鐨勬椂鍊欏垎缁勪篃鑳界敤
				.getExample();
		PageInfo<Address> pageInfo = addressService.selectByExample(example, page);
		return pageInfo;
	}
	
	@RequestMapping("queryOneById")
	public @ResponseBody Address queryOne(Model model, Address address, Page page) {
		Object example = ExampleUtil.getInstance().addParam("city",address.getCity(), Operation.LIKE)//////// 宸ュ叿閲岄潰鏈夋敞閲�
				.addOrder("id")////// 鎺掑簭 鑷繁鎵嬪啓鍒嗙粍鐨勬椂鍊欏垎缁勪篃鑳界敤
				.getExample();
		Address pageInfo = addressService.selectOne(address);
		return pageInfo;
	}
	
	
	/**
	 * 鏌ヨ鍟嗗搧鍒楄〃
	 * 
	 * @author suae
	 * @Date  2016骞�7鏈�6鏃�20:34
	 * 
	 * @param model
	 * @param Goods
	 * @param page
	 * @return
	 */
	@RequestMapping("queryListByUserId")
	public @ResponseBody List<Address> queryByUserId(Model model, Address address, Page page) {
		
		List<Address> pageInfo = addressService.queryListByUserId(address);
		return pageInfo;
	}
	
	/**
	 * 添加地址
	 * @param model
	 * @param address
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("addAddress")
	public @ResponseBody int addAddress(Model model,Address address) throws Exception{
		int n = 0 ;
		while(n < 100000){
		n = (int)(Math.random()*1000000);
		}		
		address.setId(n);
		int statusCode = addressService.insert(address);
		
		return statusCode;
		
	}
	
	
	/**
	 * 删除一个地址
	 * @param model
	 * @param address
	 * @return
	 */
	@RequestMapping("deleteOneEntity")
	public @ResponseBody int deleteOne(Model model,Address address){
		
		int statusCode = addressService.delete(address);
	
		return statusCode;
	}
	
	/**
	 * 删除地址列表
	 * @param model
	 * @param list
	 * @return
	 */
	@RequestMapping("deleteAddressList")
    public @ResponseBody int deleteAddresses(Model model,List list){
		
		int statusCode = addressService.deleteByPrimaryKeys((java.util.List<?>) list);
		return statusCode;
	}
	
	/**
	 * 修改地址信息
	 * @param model
	 * @param address
	 * @return
	 */
	@RequestMapping("updateAddress")
	public @ResponseBody int updateAddress(Model model,Address address){
			
			int statusCode = addressService.updateByPrimaryKey(address);
			return statusCode;
		}
		
}
